/*
一些和web ui有关系的逻辑
*/
$(document).ready(function(){
	$("#btnlogin").click(on_web_btnlogin_click);
	$("#btnregister").click(on_web_btnregister_click);
	$("#btnpassport").click(on_web_btnpassport_click);
});

/*
	点击标题栏的登录按钮
*/
function on_web_btnlogin_click(){
	$("#btnpassport").val('登录');
	$(".login").fadeToggle('fast');
	$("#loginname").val(uk.localdata.loginname);
	$("#loginname").focus();
}

/*
	点击标题栏的注册按钮
*/
function on_web_btnregister_click(){
	$("#btnpassport").val('注册');
	$(".login").fadeToggle('fast');
	$("#loginname").focus();
}

/*
	点击登录或者注册钮
*/
function on_web_btnpassport_click(){
	var sname = $("#loginname").val();
	var spass = $("#loginpass").val();
	if(sname == ""){
		$("#loginname").focus();
		$('.login').find('.errtip').text('请输入用户名');
		return;
	}
	if(spass == ""){
		$("#loginpass").focus();
		$('.login').find('.errtip').text('请输入密码');
		return;
	}

	$('.login').fadeOut('fast');
	uk.connect(function(success){
		if(!success){
			$('.login').fadeIn('fast');
			$('.login').find('.errtip').text('连接服务器失败');
			return;
		}
		if('注册'==$("#btnpassport").val()){
			uk.netsend(uk.netevents.REGISTER,{name:sname,pass:spass});
		}else{
			uk.netsend(uk.netevents.LOGIN,{name:sname,pass:spass});
		}
	},this);
}

function on_web_passport_action_callback(d){
	if(d.success){
		localStorage.setItem('loginname',d.info.id);
		localStorage.setItem('visitor',d.visitor);
		$('.login').fadeOut('fast');
		if(d.createrole){
			if(uk.cache.hasOwnProperty('rolemap')){
				ready_create_role(uk.cache.rolemap);
			}else{
				alert('加载资源失败，请刷新后重新游戏');
			}
		}
	}else if(!uk.localdata.visitor){
		$('.login').fadeIn('fast');
		$('.login').find('.errtip').text(d.info);
	}
}

function ready_create_role(roles){
	uk.tempdata.roleselectindex = 0;
	uk.tempdata.rolelist = [];
	for(var i in uk.cache.rolemap){
		uk.tempdata.rolelist.push(uk.cache.rolemap[i]);
	}
	if(uk.tempdata.rolelist.length > 0){
		switch_role(0);
	}
	$('.createrole').fadeIn('fast');
	$('.createrole').find('.arrow-right').on('click',function(){ switch_role(uk.tempdata.roleselectindex+1); });
	$('.createrole').find('.arrow-left').on('click',function(){ switch_role(uk.tempdata.roleselectindex-1); });
}

function switch_role(idx){
	if(idx < 0 || idx >= uk.tempdata.rolelist.length)
		return;
	var role = uk.tempdata.rolelist[idx];
	uk.tempdata.currole = role;
	uk.tempdata.roleselectindex = idx;
	var frm = $('.createrole');	
	frm.find('img').attr('src', "assets/roles/"+role.name+".jpg");
	frm.find('.rolecell').on('click',on_role_cell_click);
	frm.find('.rolename').text(role.name);
	frm.find('.roledescribe').text(role.describe);
	var ulprop = frm.find(".roleinfo>ul");
	ulprop.empty();
	for(var k in role.exprops){
		ulprop.append("<li>"+k+":"+role.exprops[k]+"</li>");
	}
}

function on_role_cell_click(){
	$('.createrole').fadeOut('fast');
	uk.netsend(uk.netevents.CREATEROLE,{id:uk.tempdata.currole.id});
	uk.netbind(uk.netevents.CREATEROLE,function(obj){
		if(obj.success){
			delete uk.tempdata.roleselectindex;
			delete uk.tempdata.rolelist;
			delete uk.tempdata.currole;
			uk.logic.self = uk.create_role(obj.roleid,obj);
			uk.game.state.start('main');
		}else{
			console.log(obj.msg);
		}
	},null,false);
}